﻿using System.Windows;
using MayhemCore;
using Microsoft.Phone.Tasks;
using MayhemCore.ModuleTypes;
using System.Device.Location;
using System;

namespace WP7Modules.Reactions
{
    [MayhemModule("Open Map", "Opens the map application at your current location when triggered")]
    public class OpenMap : ReactionBase
    {
        private GeoCoordinateWatcher watcher;

        public override void Perform()
        {
            GetCoordinate();
        }

        private void GetCoordinate()
        {
            // The watcher variable was previously declared as type GeoCoordinateWatcher. 
            if (watcher == null) {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // Use high accuracy.
                watcher.MovementThreshold = 1; // Use MovementThreshold to ignore noise in the signal.
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }
            watcher.Start();
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready) {
                // Use the Position property of the GeoCoordinateWatcher object to get the current location.

                BingMapsTask task = new BingMapsTask();
                

                task.Center = watcher.Position.Location;
                //Stop the Location Service to conserve battery power.
                watcher.Stop();

                task.Show();
            }
        }
    }
}
